package gui;


import java.awt.*;
import javax.swing.*;

import java.awt.Font;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.*; // For JPanel, etc.

import agents.CentralAgent;
import building.Cell;
import building.InfoAgent;
import building.InfoGame;

import java.awt.*;           // For Graphics, etc.
import java.awt.geom.*;      // For Ellipse2D, etc.
import java.util.GregorianCalendar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


/**
 * <p><B>Title:</b> IA2-SMA</p> *
 * <p><b>Description:</b> Practical exercise 2010-11. Recycle swarm.</p>
 * Main class of the graphical interface controlled by the Central Agent.
 * It offers several methods to show the changes of the agents within the
 * city., as well as an area with the logs and an area with the main statistical
 * results.
 * <p><b>Copyright:</b> Copyright (c) 2011</p>
 * <p><b>Company:</b> Universitat Rovira i Virgili (<a
 * href="http://www.urv.cat">URV</a>)</p>
 * @author David Isern & Joan Albert L�pez
 * @version 2.0
 */
public class GraphicInterface extends JFrame
{

    int inset = 50;

    gui.MapVisualizer jMapPanel;
    private gui.LogPanel jLogPanel;
    private gui.StatisticsPanel jStatisticsPanel;

    GridLayout gridMainLayout = new GridLayout();
    JTabbedPane jGameTabbedPane = new JTabbedPane();
    JPanel jGamePanel = new JPanel();

    /*** cate **/
    public boolean start=false;

    public GregorianCalendar startTime = null;
    public InfoGame game;
    // public GregorianCalendar endTime;


//  GridLayout gridLayoutPanelPartida = new GridLayout();

//  BorderLayout borderLayout1 = new BorderLayout();
//  JPanel jPanelStatistics = new JPanel();
//  JFileChooser jFileChooserLogFile = new JFileChooser();
    JScrollPane jScrollPane1 = new JScrollPane(jGamePanel);

    public boolean stop;

    private SettingsPanel settingPanel;

    private JMenuBar jMenuBar;

    private JMenu menuFile;

    private JMenuItem openMenuItem;

    private JMenuItem saveMenuItem;

    private JMenuItem closeMenuItem;
    
    private CentralAgent central;




    public GraphicInterface(InfoGame p, CentralAgent central)
    {
        try
        {
            int floors = p.nfloors;
            this.central = central;
            jbInit();
            //for(int i=0; i<floors; i++)
            this.showGameMap(p);
            

            //this.showGameMap(p.getMap(0));
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception
    {

        try
        {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        }
        catch (Exception e)
        {
            System.err.println("-> We use the default L&F. Reason: "+e.toString());
        }

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds( inset, inset, screenSize.width - inset * 2, screenSize.height - inset * 2 );
        //Quit this app when the big window closes.
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                //System.exit(0);
                dispose();
            }
        });

        this.getContentPane().setLayout(gridMainLayout);
//    this.getContentPane().setBackground(Color.BLACK);
        this.setForeground(Color.WHITE);
//    this.setBackground(Color.BLACK);
        this.setTitle("Fire Evacuation");

        gridMainLayout.setColumns(1);




        /****************************************************/

        Font fontLabels = new Font("Arial", Font.PLAIN, 12);
        Font fontTextArea = new Font("Arial", Font.PLAIN, 12);
        Font font = new Font("Arial", Font.PLAIN, 12);


        jGameTabbedPane.setTabPlacement(JTabbedPane.TOP);

        jGameTabbedPane.setBackground(UtilsGUI.colorBackgroundFrame);
        jGameTabbedPane.setForeground(UtilsGUI.colorForegroundText);
        jGameTabbedPane.setFont(new java.awt.Font("Arial", Font.PLAIN, 10));
        jGameTabbedPane.setBorder(BorderFactory.createEtchedBorder());
        jGameTabbedPane.setMinimumSize(new Dimension(1280, 768));
        jGameTabbedPane.setPreferredSize(new Dimension(1280, 768));


        /** Partida *****************************/
        jGamePanel = new JPanel(new GridLayout(1,1));
        jGamePanel.setBackground(UtilsGUI.colorBackgroundFrame);
        jGamePanel.setForeground(UtilsGUI.colorForegroundText);
        jGamePanel.setBorder(BorderFactory.createEtchedBorder());
        jGamePanel.setMinimumSize(new Dimension(1280, 768));
        jGamePanel.setPreferredSize(new Dimension(1280, 768));
        pack();
        jScrollPane1.setVisible(true);
        //   jScrollPane1.setVisible(true);

        ImageIcon icon = new ImageIcon(UtilsGUI.pathIconPartida);
        jGameTabbedPane.addTab("Partida", icon, jGamePanel);


        /** Logs *****************************/
        this.jLogPanel = new LogPanel();
        icon = new ImageIcon(UtilsGUI.pathIconLogs);
        jGameTabbedPane.addTab("Logs", icon, this.jLogPanel);

        this.jLogPanel.showMessage("Initializing components ....\n");

        /** Statistics **********************/
        this.jStatisticsPanel = new StatisticsPanel();
        jGameTabbedPane.addTab("Statistics", icon, this.jStatisticsPanel);

        this.jStatisticsPanel.showMessage("All tabs initialized successfully!");

        /** TABBED PANE *********************/
        this.getContentPane().add(jGameTabbedPane);

    } //endof jbInit()


  


    public void showGameMap(InfoGame p)
    {
        //for(int i=0; i<floors; i++)

        this.jMapPanel = new MapVisualizer(p.getMap(), p.getInfo());
        // aggiungere il settingPanel al jgamepanel.

        this.settingPanel = new SettingsPanel(p, this);
        //settingPanel.setPreferredSize(new Dimension(300, 100));
        this.jGamePanel.add(settingPanel, BorderLayout.WEST);
        this.jGamePanel.add(jMapPanel);
        this.pack();

        this.jGamePanel.repaint();
    }

    public void initGUI(final InfoGame game)
    {
    }

    /**
     * Mostra una cadena en el panell destinat a logs
     * @param msg String per mostrar
     */
    public void showLog(String msg)
    {
        this.jLogPanel.showMessage(msg);
    }


    /**
     * Mostra una cadena en el panell destinat a stad�stiques
     * @param msg String per mostrar
     */
    public void showStatistics(String msg)
    {
        this.jStatisticsPanel.showMessage(msg+"\n");
    }



    public static void main(String[] args)
    {
        GraphicInterface graphicinterface = new GraphicInterface(null,null);
        graphicinterface.setVisible(true);
        //   graphicinterface.initGUI(null);

    }

    @Override
    public void paint(Graphics g)
    {
        jGamePanel.paintComponents(g);
        super.paint(g);
    }


    public void setStart(boolean b)
    {
        if (b)
        {
            this.jMapPanel.setStartTime();
            this.startTime = new GregorianCalendar();
            start = true;
            
        }

    }

    public void setStop(boolean b)
    {
        this.jMapPanel.setStopTime();
        stop = true;
    }



    public void mainLoopOnes()
    {
        while(jMapPanel.getInfo().getTurn()<500)
        {

            this.settingPanel.update();
        }
    }

    public String showTime()
    {

        return this.jMapPanel.getInfo().getElapsedTime();

    }

    public String getFirePhase()
    {
        // TODO Auto-generated method stub
        String s = null;
        this.jMapPanel.getInfo().setFirePhase();
        if (this.jMapPanel.getInfo().isIncipient())
            s = "incipient";
        if(this.jMapPanel.getInfo().isFree_burning())
            s = "free_burning";
        if(this.jMapPanel.getInfo().isSmoldering())
            s = "smoldering";
        return s;
    }

    public String getNTurn()
    {
        // TODO Auto-generated method stub
        return " " +this.jMapPanel.getInfo().getTurn();
    }

    public boolean isGameFinished()
    {
        return this.jMapPanel.getInfo().isGameFinished();
    }

	public CentralAgent getCentral() {
		return central;
	}

	public void setCentral(CentralAgent central) {
		this.central = central;
	}



}
